﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JYH.Data;
using Newtonsoft.Json;
using JYH.Model;
using JYH.DataAccess;
using JYH.Framework;

namespace JYH.Business
{
    /// <summary>
    ///  SysFormNOInfo
    /// </summary>
    public class SysFormNOInfoBLL : BusinessBase<SysFormNOInfo>
    {
        /// <summary>
        ///  SysFormNOInfo
        /// </summary>
        public SysFormNOInfoBLL()
        {
            myDataBase = DB.D();
            this.SqlSelect = @"; Select SysFormNOInfo.* From SysFormNOInfo";
        }

        /// <summary>
        /// 获取最新编号
        /// </summary>
        /// <param name="FormNO">表单编号(必填字段)</param>
        /// <param name="DateNO">日期格式（可填字段,如填写则作为检索条件）</param>
        /// <param name="LengthNO">流水号长度（可填字段,如填写则更新原先设置值）</param>
        /// <param name="FormName">单号说明（可填字段,如填写则更新原先设置值）</param>
        /// <param name="PrefixNO">前缀字符（可填字段,如填写则作为检索条件）</param>
        /// <param name="SuffixNO">后缀字符（可填字段,如填写则作为检索条件）</param>
        /// <returns></returns>
        public static string GetLastFormNO(string FormNO, out string Msg, string FormName = null, string PrefixNO = null, string SuffixNO = null, string DateNO = null, int LengthNO = 0)
        {
            Msg = string.Empty;
            string LastNO = string.Empty;
            Database db = DB.D();
            db.BeginTransaction();
            StringBuilder sb = new StringBuilder();
            try
            {
                Sql sql = new Sql();
                sql.Append("Select SysFormNOInfo.* From SysFormNOInfo");
                sql.Where("SysFormNOInfo.FormNO='" + FormNO + "'");
                if (!string.IsNullOrEmpty(PrefixNO))
                {
                    sql.Where("SysFormNOInfo.PrefixNO='" + PrefixNO + "'");
                }
                if (!string.IsNullOrEmpty(SuffixNO))
                {
                    sql.Where("SysFormNOInfo.SuffixNO='" + SuffixNO + "'");
                }
                if (!string.IsNullOrEmpty(DateNO))
                {
                    sql.Where("SysFormNOInfo.DateNO='" + DateNO + "'");
                }
                sql.OrderBy("SysFormNOInfo.UpdateTime Desc ,SysFormNOInfo.CreateTime Desc");
                List<SysFormNOInfo> list = db.Fetch<SysFormNOInfo>(sql.SQL, null);
                if (list == null || list.Count == 0)
                {
                    SysFormNOInfo obj = new SysFormNOInfo();
                    obj.FormNO = FormNO;
                    obj.PrefixNO = PrefixNO;
                    obj.SuffixNO = SuffixNO;
                    if (string.IsNullOrEmpty(DateNO))
                    {
                        DateNO = "yyyyMMdd";
                    }
                    obj.DateNO = DateNO;
                    obj.RState = "1";
                    obj.FormNOID = Guid.NewGuid().ToString();
                    if (LengthNO == 0)
                    {
                        LengthNO = 6;
                    }
                    obj.LengthNO = LengthNO;
                    obj.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    obj.FormName = FormName;
                    obj.LastNO = PrefixNO + DateTime.Now.ToString(DateNO) + 1.ToString().PadLeft(LengthNO, '0') + SuffixNO;
                    sb.Append(BusinessTool.GetInsertSql(obj));
                    LastNO = obj.LastNO;
                }
                else
                {
                    SysFormNOInfo obj = list[0];
                    int qty = (int)obj.LengthNO;
                    int suf = 0;
                    if (!string.IsNullOrEmpty(obj.SuffixNO))
                    {
                        suf = obj.SuffixNO.Length;
                    }

                    string no = obj.LastNO.Substring(obj.LastNO.Length - qty - suf, qty);
                    int index = 1;
                    int.TryParse(no, out index);
                    if (string.Equals(obj.LastNO, obj.PrefixNO + DateTime.Now.ToString(obj.DateNO) + index.ToString().PadLeft((int)obj.LengthNO, '0') + obj.SuffixNO))
                    {
                        index += 1;
                    }
                    else
                    {
                        index = 1;
                    }
                    if (LengthNO > 0)
                    {
                        obj.LengthNO = LengthNO;
                    }

                    obj.LastNO = obj.PrefixNO + DateTime.Now.ToString(obj.DateNO) + index.ToString().PadLeft((int)obj.LengthNO, '0') + obj.SuffixNO;

                    if (!string.IsNullOrEmpty(FormName))
                    {
                        obj.FormName = FormName;
                    }

                    obj.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    sb.Append(BusinessTool.GetUpdateSql(obj));
                    LastNO = obj.LastNO;
                }

                if (sb.Length > 0)
                {
                    db.Execute(sb.ToString());
                }
                db.CompleteTransaction();
                return LastNO;
            }
            catch (Exception ex)
            {
                Msg = ex.Message;
                db.AbortTransaction();
                return null;
            }
        }

        /// <summary>
        /// 获取单号对应编号
        /// 0 收料入库单   1 收料退库单   2 生产领料单    3 收料退库单
        /// 4 生产退料单   5 物料调拨单   6 其他领料单    7 其他入库单
        /// 8 生产缴库单   9 生产退库单   10 销售出库单   11 销售退库单  12 盘盈盘库单 13 生产备料单
        /// </summary>
        /// <param name="TradeType"></param>
        /// <returns></returns>
        public static WmsTradeOrderInfo GetFormName(WmsTradeOrderInfo Trade)
        {
            WmsTradeOrderInfo info = new WmsTradeOrderInfo();
            switch (Trade.TradeType)
            {
                case 0:
                    info.Remark = "WE";
                    info.TradeType = Trade.TradeType;
                    info.TradeOrderNO = "TradeOrderNO_WE";
                    return info;
                case 3:
                    info.Remark = "MFG";
                    info.TradeType = Trade.TradeType;
                    info.TradeOrderNO = "TradeOrderNO_MFG";
                    return info;
                case 5:
                    info.Remark = "DB";
                    info.TradeType = Trade.TradeType;
                    info.TradeOrderNO = "TradeOrderNO_DB";
                    return info;

                case 13:
                    info.Remark = "MP";
                    info.TradeType = Trade.TradeType;
                    info.TradeOrderNO = "TradeOrderNO_MP";
                    return info;

                default:
                    info.Remark = "DB";
                    info.TradeType = Trade.TradeType;
                    info.TradeType = Trade.TradeType;
                    info.TradeOrderNO = "TradeOrderNO_DB";
                    return info;
            }
        }
    }
}